package com.yksj.healthtalk.utils;

import org.json.JSONArray;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

public class ContactsUtils {
	
	/**
	 * 获取当前手机的通讯录
	 */
	public static String getContacts(Context context){
		Cursor cur = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
		JSONArray jsonArr = new JSONArray();
		//ArrayList<String> list = new ArrayList<String>();
		while (cur.moveToNext()) {
			int columnId = cur.getColumnIndex(ContactsContract.Contacts._ID);
			int columnName = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
			int columnPoneCount = cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
			
			String id = cur.getString(columnId);
			String name  = cur.getString(columnName);  //获取名称
			int count = cur.getInt(columnPoneCount);
			if(count > 0){
				Cursor phone = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id, null, null);
				int columnPhoneNumber =  phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
				int columnPhoneType = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
				while(phone.moveToNext()){
					String phoneNumber = phone.getString(columnPhoneNumber);   //获取手机号
					String phoneType = phone.getString(columnPhoneType);    //获取类型
					jsonArr.put(name+","+phoneNumber);
				}
				phone.close();
			}
		}
		cur.close();
		return jsonArr.toString();
	}

}
